package cn.jdy.anquan.admin;

import cn.jdy.anquan.entity.CompanySafe;
import cn.jdy.anquan.service.CompanySafeService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyuyong
 * @title 中小企业资料库控制类
 * @date 2015-06-01
 */
@Controller
@RequestMapping("/companySafe")
public class CompanySafeCtrl extends BaseCtrl {

  @Autowired
  private CompanySafeService companySafeService;

  /**
   * 初始化
   */
  @RequestMapping("/list")
  public ModelAndView list(CompanySafe companySafe) {
    ModelAndView mv = new ModelAndView("/companySafe/companySafeList.jsp");
    mv.addObject("entity", companySafe);
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_BUSINESS, 0L));//商品分类下拉列表
    return mv;
  }
  /**
   * 初始化
   */
  @RequestMapping("/edit/{id}")
  public ModelAndView edit(@PathVariable("id")Integer id) {
    ModelAndView mv = new ModelAndView("/companySafe/companySafeDetail.jsp");
    mv.addObject("entity", companySafeService.find(id));
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_BUSINESS, 0L));//商品分类下拉列表
    return mv;
  }
  /**
   * 初始化
   */
  @RequestMapping("/save")
  public ModelAndView save(CompanySafe companySafe) {
    companySafeService.saveOrUpdate(companySafe);
    return list(null);
  }

  /**
   * 查询
   * @param companySafe
   * @return
   */
  @RequestMapping("/queryByPage")
  @ResponseBody
  public Map<String, Object> queryByPage(CompanySafe companySafe, String fromDate, String toDate) {
    Map<String, Object> jsonMap = new HashMap<>();
    try {
      jsonMap.putAll(companySafeService.search(companySafe, fromDate, toDate));
      Long count = companySafeService.count();
      jsonMap.put("iTotalDisplayRecords", count);
      jsonMap.put("iTotalRecords", count);
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询错误", e);
    }
    return jsonMap;
  }
}
